package com.myProject.control;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myProject.bean.TNote;
import com.myProject.bean.TUser;
import com.myProject.serves.Impl.LogSevImpl;
import com.myProject.serves.Impl.UserSevImpl;
import com.myProject.serves.LogSev;
import com.myProject.serves.UserSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    UserSev us = new UserSevImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {
            showAll(request, response);
        } else if ("userLogin".equalsIgnoreCase(med)) {
            userLogin(request, response);
        } else if ("signOut".equalsIgnoreCase(med)) {
            userSignOut(request, response);
        }
    }

    private void userSignOut(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        String user = "";
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("loginName".equals(cookie.getName())) {
                    //cookie名字要相同
                    cookie = new Cookie("loginName", null);
                    //时间耗尽
                    cookie.setMaxAge(0);
                    // 相同路径
                    cookie.setPath(request.getContextPath());
                    response.addCookie(cookie);
                }
            }
        }
    }

    private void userLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        TUser tUser = new TUser();
        tUser.setUser(username);
        tUser.setPassword(password);
        List<TUser> list = us.loginUser(tUser);

        if (list.size() == 0) {
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString("检查你的密码或账号哪里错了？");
            System.out.println("json = " + json);
            pw.write(json);
        } else {
            System.out.println("list = " + list);
            Cookie cookie = new Cookie("loginName", list.get(0).getUser());
            response.addCookie(cookie);
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString("Hello");
            System.out.println("json = " + json);
            pw.write(json);


            LogSev ls = new LogSevImpl();
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TNote t = new TNote();
            System.out.println("cookie.getValue() = " + cookie.getValue());
            String user = cookie.getValue();
            t.setNote("用户" + user + "进行了登录操作!");
            t.setTime(d);
            ls.addLog(t);
        }
    }

    private void addUserLog(HttpServletRequest request, HttpServletResponse response) {
        LogSev ls = new LogSevImpl();
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TNote t = new TNote();
        Cookie[] cookies = request.getCookies();
        String user = "";
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("loginName".equals(cookie.getName())) {
                    System.out.println("cookie.getValue() = " + cookie.getValue());
                    user = cookie.getValue();
                    t.setNote("用户" + user + "进行了登录操作!");
                    t.setTime(d);
                    ls.addLog(t);
                }
            }
        }
    }


    private void showAll(HttpServletRequest request, HttpServletResponse response) {

    }
}
